﻿using System.Collections.Generic;

namespace Baggr.FeedParser.Atom
{
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Xml.Serialization;

    [Serializable]
    [DebuggerStepThrough]
    [DesignerCategory("code")]
    [XmlType(Namespace = "http://www.w3.org/2005/Atom")]
    [XmlRoot("feed", Namespace = "http://www.w3.org/2005/Atom", IsNullable = false)]
    public class AtomFeed
        :IFeed
    {
        [XmlElement("author")]
        public AtomPerson Author { get; set; }

        [XmlElement("category")]
        public AtomCategory Category { get; set; }

        [XmlElement("contributor")]
        public AtomPerson Contributor { get; set; }

        [XmlElement("entry")]
        public AtomEntity[] Entries { get; set; }

        [XmlElement("generator")]
        public AtomGenerator Generator { get; set; }

        [XmlElement("icon")]
        public string Icon { get; set; }

        [XmlElement("id")]
        public string Id { get; set; }

        /// <summary>
        /// Заглавие ленты.
        /// </summary>
        string IFeed.Title
        {
            get
            {
                if (Title == null)
                    return string.Empty;
                return Title.Text ?? string.Empty;
            }
        }

        /// <summary>
        /// Ссылка на сайт ленты.
        /// </summary>
        Uri IFeed.Link
        {
            get
            {
                if (Link == null)
                    return null;
                if (string.IsNullOrEmpty(Link.Href))
                    return null;
                return new Uri(Link.Href);
            }
        }

        /// <summary>
        /// Описание ленты.
        /// </summary>
        string IFeed.Description
        {
            get
            {
                if (Subtitle == null)
                    return string.Empty;
                return Subtitle.Text ?? string.Empty;
            }
        }

        /// <summary>
        /// Дата публикации.
        /// </summary>
        DateTime IFeed.PublicationDate
        {
            get
            {
                if (string.IsNullOrEmpty(Updated))
                    return new DateTime();

                return DateTime.Parse(Updated);
            }
        }

        /// <summary>
        /// Изображение ленты.
        /// </summary>
        Uri IFeed.ImageUrl
        {
            get
            {
                if (string.IsNullOrEmpty(Icon))
                    return null;

                return new Uri(Icon);
            }
        }

        /// <summary>
        /// Записи ленты.
        /// </summary>
        IEnumerable<IFeedItem> IFeed.Items
        {
            get
            {
                return Entries;
            }
        }

        [XmlElement("link")]
        public AtomLink Link { get; set; }

        [XmlElement("logo")]
        public string Logo { get; set; }

        [XmlElement("rights")]
        public AtomText Rights { get; set; }

        [XmlElement("subtitle")]
        public AtomText Subtitle { get; set; }

        [XmlElement("title")]
        public AtomText Title { get; set; }

        [XmlElement("updated")]
        public string Updated { get; set; }
    }
}
